package domain

import (
	"context"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
	"zhongxc.cc/mall-common/msdb"
	"zhongxc.cc/mall-product/internal/dao"
	"zhongxc.cc/mall-product/internal/model"
	"zhongxc.cc/mall-product/internal/repo"
)

type PmsSkuInfoDomain struct {
	pmsSkuInfoRepo repo.PmsSkuInfoRepo
}

func NewPmsSkuInfoDomain(db *msdb.MsDB) *PmsSkuInfoDomain {
	return &PmsSkuInfoDomain{
		pmsSkuInfoRepo: dao.NewPmsSkuInfoDao(db),
	}
}

func (p *PmsSkuInfoDomain) FindById(ctx context.Context, skuId int64) (*model.PmsSkuInfo, error) {
	skuInfo, err := p.pmsSkuInfoRepo.FindById(ctx, skuId)
	if err != nil {
		logx.Error(err)
		return nil, errors.New("数据库异常")
	}
	return skuInfo, nil
}

func (p *PmsSkuInfoDomain) Save(ctx context.Context, skuInfo *model.PmsSkuInfo) error {
	err := p.pmsSkuInfoRepo.Save(ctx, skuInfo)
	if err != nil {
		logx.Error(err)
		return errors.New("数据库异常，注册失败")
	}
	return nil
}

func (p *PmsSkuInfoDomain) UpdateSaleCount(ctx context.Context, skuId int64, saleCount int64) error {
	err := p.pmsSkuInfoRepo.UpdateSaleCount(ctx, skuId, saleCount)
	if err != nil {
		logx.Error(err)
		return errors.New("数据库异常，销量更新失败")
	}
	return nil
}
